//
//  PhoneLoginViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/2/27.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "PhoneLoginViewController.h"
#import "SetBlack.h"
#import "BlackManager.h"
@interface PhoneLoginViewController ()<UITextFieldDelegate>
@property (nonatomic, retain)UITextField *phoneInput;//手机账号
@property (nonatomic, retain)UITextField *checkInput;//密码

@end

@implementation PhoneLoginViewController
-(void)dealloc
{
    [_phoneInput release];
    [_checkInput release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //手机号输入框
    self.phoneInput = [[UITextField alloc]initWithFrame:CGRectMake(10, self.view.frame.size.height / 6, self.view.frame.size.width - 20, self.view.frame.size.height / 14)];
    _phoneInput.delegate = self;
    _phoneInput.tag = 1011;
    _phoneInput.layer.cornerRadius = 10;
    _phoneInput.clipsToBounds = YES;
    _phoneInput.placeholder = @"请输入手机号";
    _phoneInput.backgroundColor = [UIColor lightGrayColor];
    _phoneInput.clearButtonMode = UITextFieldViewModeAlways;//只要开始输入就有删除的叉号
    [self.view addSubview:_phoneInput];
    [_phoneInput release];
    //密码输入框
    self.checkInput = [[UITextField alloc]initWithFrame:CGRectMake(10, _phoneInput.frame.origin.y + _phoneInput.frame.size.height + 10, self.view.frame.size.width - 20, self.view.frame.size.height / 14)];
    _checkInput.tag = 2022;
    _checkInput.delegate = self;
    _checkInput.layer.cornerRadius = 10;
    _checkInput.clipsToBounds = YES;
    _checkInput.secureTextEntry = YES;
    _checkInput.placeholder = @"请输入密码";
    _checkInput.backgroundColor = [UIColor lightGrayColor];
    _checkInput.clearButtonMode = UITextFieldViewModeAlways;//只要开始输入就有删除的叉号
    [self.view addSubview:_checkInput];
    [_checkInput release];
    
    //登录按钮
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(self.view.frame.size.width / 5, _checkInput.frame.origin.y + _checkInput.frame.size.height + 100, self.view.frame.size.width / 5 * 3, self.view.frame.size.height / 14);
    [loginBtn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    loginBtn.layer.cornerRadius = 10;
    loginBtn.clipsToBounds = YES;
    [loginBtn setBackgroundColor:[UIColor redColor]];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:loginBtn];
}
#pragma mark - 夜间模式设置
//设置夜间模式  重写BaseViewController中的方法
-(void)setNightDayModel
{
    [SetBlack setBackgroundColorWithView:self.view];
}
-(void)setLightDayModel
{
    [SetBlack setBackgroundColorWithView:self.view];
    
}
-(void)loginAction
{
    //从本地路径中解档数据
    NSString *namePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"name.a"];
    NSMutableArray *nameArr = [NSKeyedUnarchiver unarchiveObjectWithFile:namePath];
    NSString *pwdPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"pwd.a"];
    NSMutableArray *pwdArr = [NSKeyedUnarchiver unarchiveObjectWithFile:pwdPath];
    //记录数据在数组中的位置,通过姓名获取到的位置即密码和照片在各自数组中的位置
    NSInteger i = 0;
    //判断账户是否存在
    if ([nameArr containsObject:_phoneInput.text]) {
        for (i; i < nameArr.count; i++) {
            if ([_phoneInput.text isEqualToString:nameArr[i]]) {
                NSLog(@"%ld", i);
                break;
            }
        }
        //把用户信息在数组中的位置放在单例里存储
        NSNumber *index = [NSNumber numberWithInteger:i];
        [[NSUserDefaults standardUserDefaults]setObject:index forKey:@"index"];
        //判断密码是否与账号对应
        if ([pwdArr[i] isEqualToString:_checkInput.text]) {
            //如果账号已经存在
            //设置名字为单例
            [[NSUserDefaults standardUserDefaults]setObject:_phoneInput.text forKey:@"name"];
            //设置账号的图片为现在单例内容
            NSString *picturePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"picture.a"];
            NSMutableArray *pictureArr = [NSKeyedUnarchiver unarchiveObjectWithFile:picturePath];
            NSNumber *i = [[NSUserDefaults standardUserDefaults]objectForKey:@"index"];
            NSInteger index = i.intValue;
            NSData *data = [pictureArr objectAtIndex:index];
            [[NSUserDefaults standardUserDefaults]setObject:data forKey:@"img"];
            
            //设置登陆状态
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"register"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            NSLog(@"用户名或密码错误");
            //弹窗
            UIAlertController *promptAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账户密码不匹配" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:promptAlert animated:YES completion:^{
                
            }];
            [self dismissViewControllerAnimated:YES completion:^{
                //
            }];
            
        }
    }else{
        //弹窗
        UIAlertController *promptAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账户不存在" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:promptAlert animated:YES completion:^{
            
        }];
        [self dismissViewControllerAnimated:YES completion:^{
            //
        }];
        
    }
    [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"OtherLogin"];
}
#pragma mark - 回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if (textField.tag == 1011) {
        [textField resignFirstResponder];
        [_checkInput becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
    return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag == 2022) {
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y - 20);
    }
    return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag == 2022) {
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 20);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
